package com.kuyun.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuyun.entity.News;
import com.kuyun.service.NewsService;

@Controller
@RequestMapping(value = "/news")
public class NewsController {

	@Resource
	private NewsService newsService;

	// 获得所有微博趣事
	@RequestMapping(value = "/getNews", method = RequestMethod.POST)
	@ResponseBody
	public List<News> getNews(HttpSession session, Model model) {
		List<News> newsList = newsService.findAllNews();
		Integer i = 1;
		for (News news : newsList) {
			news.setSequnce(i);
			i++;
		}
		return newsList;
	}

	// 微博趣事管理
	@RequestMapping(value = "/showNews")
	public String showFunsType(HttpSession session, Model model) {
		List<News> newsList = newsService.findAllNews();
		Integer i = 1;
		for (News news : newsList) {
			news.setSequnce(i);
			i++;
		}
		model.addAttribute("newsList", newsList);
		return "news";
	}

	// 趣事删除
	@RequestMapping(value = "/delNews", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> delNews(Integer newsId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean flag = newsService.delNews(newsId);
		if (flag) {
			resultMap.put("result", "true");
		} else {
			resultMap.put("result", "false");
		}
		return resultMap;
	}

}
